import cacheKeys from '@/common/app/cacheKeys.js'
import { localStorage } from '@/common/app/storage.js'

export default {
  state: {
    // 当前登录的用户的会话信息
    userToken: localStorage.getObject(cacheKeys.CACHE_USER_TOKEN) || null,
    // 当前登录的用户信息，只保存用户基本信息
    userInfo: localStorage.getObject(cacheKeys.CACHE_USER_INFO) || null,
    // 验证码的倒计时
    codeTime: localStorage.getNumber(cacheKeys.CACHE_CODE_TIME) || 0
  },
  getters: {
    userToken(state) {
      return state.userToken
    },
    userInfo(state) {
      return state.userInfo
    },
    codeTime(state) {
      return state.codeTime
    }
  },
  mutations: {
    [cacheKeys.CACHE_USER_TOKEN](state, data) {
      state.userToken = data;
      localStorage.setObject(cacheKeys.CACHE_USER_TOKEN, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_USER_TOKEN)
      }
    },
    [cacheKeys.CACHE_USER_INFO](state, data) {
      state.userInfo = data;
      localStorage.setObject(cacheKeys.CACHE_USER_INFO, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_USER_INFO)
      }
    },
    [cacheKeys.CACHE_CODE_TIME](state, data) {
      state.codeTime = data;
      localStorage.setNumber(cacheKeys.CACHE_CODE_TIME, data)
      if (data === undefined || data === null || data === '') {
        localStorage.removeItem(cacheKeys.CACHE_CODE_TIME)
      }
    }
  }
}
